package com.hefei.api.customer.manager.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hefei.api.customer.client.CustomerUrlConstants;
import com.hefei.api.customer.manager.ISystemdefManager;
import com.hefei.api.customer.vo.SysMenu;
import com.hefei.api.customer.vo.Systemdef;
import com.hefei.api.customer.vo.easyui.PageHelper;
import com.hefei.common.exception.ClientException;
import com.hefei.common.http.client.ClientFactory;
import com.hefei.common.http.request.RequestHead;
import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.http.util.SendRequest;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;

public class SystemdefManager implements ISystemdefManager {
	
	private Logger logger = Logger.getLogger(SystemdefManager.class);

	@Override
	public List<Systemdef> getAll(PageHelper page) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("page", page);
			String result = SendRequest.send(CustomerUrlConstants.SYSDEF_GET_ALL, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				List<Systemdef> Systemdef = JacksonUtil.jsonToCollections(listStr, List.class, Systemdef.class);
				return Systemdef;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public Systemdef getSystemdefById(Integer id) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("id", id);
			String result = SendRequest.send(CustomerUrlConstants.SYSDEF_GET_BY_ID, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				String listStr = JacksonUtil.beanToJson(baseResponse.getReturnObject());
				Systemdef Systemdef = JacksonUtil.jsonToCollections(listStr, Systemdef.class);
				return Systemdef;
			}else{
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public void insertSystemdef(Systemdef systemdef) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("systemdef", systemdef);
			String result = SendRequest.send(CustomerUrlConstants.SYSDEF_INSERT, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}

	@Override
	public void updateSystemdef(Systemdef systemdef) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.putPOJO("systemdef", systemdef);
			String result = SendRequest.send(CustomerUrlConstants.SYSDEF_UPDATE, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}

	@Override
	public void deleteSystemdefById(Integer id) throws ClientException {
		long timer = System.currentTimeMillis();
		logger.info("time:" + timer );
		RequestHead head = null;
		try {
			head = RequestHead.get(timer,ClientFactory.client);
			ObjectNode dataJson = JacksonUtil.createObjectNode();
			dataJson.put("id", id);
			String result = SendRequest.send(CustomerUrlConstants.SYSDEF_DELETE_BY_ID, dataJson, timer, head);
			
	 		logger.info(" time:" + timer + " result:" + result);
			logger.info( head.getLoggerStr() + " cost:" + (System.currentTimeMillis() - timer));
			
			BaseResponse baseResponse = JacksonUtil.jsonToBean(result, BaseResponse.class);
			if(!ReturnCode.RETURN_CODE_SUCCESS.equals(baseResponse.getReturnCode())){
				throw new ClientException(baseResponse.getReturnCode());
			}
		}catch(Exception e){
			logger.error("error", e);
			throw new ClientException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}

}
